package com.xbongbong.saas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.pro.enums.AmountFlagEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 关联产品展示字段枚举类
 * 注意：
 * 1.强制：该枚举的saasAttr不允许随意修改，因为要与ProducEnum中的对应枚举的saasAttr对应，程序中有用到这层对应关系
 * 2.建议：该枚举的saasAttr是最通用的，如果有特殊需要修改的，在程序中做修改
 *
 * @author chaoxiong.lei
 * @date 2019/1/14 13:57
 */
@Getter
public enum RelativeProductEnum {
    // 产品需要展示的字段
    PRODUCT_ID("num_1", "enum.relativeProduct.attrName.product_id", "productId", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9999, 1, 1, 0, 0, AmountFlagEnum.NO.getType()),
    NAME("text_1", "enum.relativeProduct.attrName.name", "name", FieldTypeEnum.TEXT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, 0, 0, AmountFlagEnum.NO.getType()),
    PRODUCT_NO("text_2", "enum.relativeProduct.attrName.product_no", "productNo", FieldTypeEnum.TEXT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 1, 0, 0, AmountFlagEnum.NO.getType()),
    BARCODE("text_3", "enum.relativeProduct.attrName.barcode", "barcode", FieldTypeEnum.TEXT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, 0, 0, AmountFlagEnum.NO.getType()),

    WAREHOUSE("text_4", "enum.relativeProduct.attrName.warehouse", "warehouse", 0, 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 1, 0, 1, AmountFlagEnum.NO.getType()),
    UNIT_NUM("num_11", "enum.relativeProduct.attrName.unit_num", "unitNum", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9955, 1, 1, 0, 0, AmountFlagEnum.NO.getType()),
    NUM("num_2", "enum.relativeProduct.attrName.num", "num", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 1, 0, 1, AmountFlagEnum.YES.getType()),

    SPECIFICATION("text_5", "enum.relativeProduct.attrName.specification", "specification", FieldTypeEnum.SPECIFICATION.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),
    STOCK("text_6", "enum.relativeProduct.attrName.stock", "stock", FieldTypeEnum.PRODUCT_STOCK.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),
    COST("num_3", "enum.relativeProduct.attrName.cost", "cost", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 0, 0, 1, AmountFlagEnum.YES.getType()),
    UNIT("text_7", "enum.relativeProduct.attrName.unit", "unit", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),

    // 库存产品字段,
    PRODUCE_DATE("date_1", "enum.relativeProduct.attrName.produce_date", "produceDate", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9900, 0, 0, 0, 1, AmountFlagEnum.NO.getType()),
    GUARANTEE_PERIOD("num_4", "enum.relativeProduct.attrName.guarantee_period", "guaranteePeriod", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9890, 0, 0, 0, 1, AmountFlagEnum.NO.getType()),
    BATCH("text_8", "enum.relativeProduct.attrName.batch", "batch", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9880, 0, 0, 0, 1, AmountFlagEnum.NO.getType()),
    MEMO("text_9", "enum.relativeProduct.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9870, 0, 0, 0, 1, AmountFlagEnum.NO.getType()),

    // 出库单销售相关字段
    SALE_PRODUCT_PRICE("num_5", "enum.relativeProduct.attrName.sale_product_price", "saleProductPrice", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9860, 1, 0, 0, 0, AmountFlagEnum.YES.getType()),
    SALE_SUBTOTAL("num_6", "enum.relativeProduct.attrName.sale_subtotal", "saleSubtotal", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9850, 1, 0, 0, 0, AmountFlagEnum.YES.getType()),
    PRODUCT_PRICE("num_7", "enum.relativeProduct.attrName.product_price", "productPrice", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9840, 1, 0, 0, 0, AmountFlagEnum.YES.getType()),
    SALE_DISCOUNT("num_8", "enum.relativeProduct.attrName.sale_discount", "saleDiscount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9830, 1, 0, 0, 0, AmountFlagEnum.YES.getType()),

    // 生产单相关字段,
    ATTRITION_RATE("num_9", "enum.relativeProduct.attrName.attrition_rate", "attritionRate", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9820, 1, 1, 0, 1, AmountFlagEnum.NO.getType()),

    SUBTOTAL("num_10", "enum.relativeProduct.attrName.subtotal", "subtotal", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9810, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),
    STATUS("text_10", "enum.relativeProduct.attrName.status", "outstock", FieldTypeEnum.TEXT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9800, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),
    LACK_NUM("num_12", "enum.relativeProduct.attrName.lack_num", "lackNum", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),
    THUMBNAIL("file_1", "enum.relativeProduct.attrName.thumbnail", "thumbnail", FieldTypeEnum.IMAGE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9780, 1, 0, 0,0, AmountFlagEnum.NO.getType()),
    //关联表的id
    REF_ID("num_13", "enum.relativeProduct.attrName.ref_id", "refId", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.NONE.getCode(), 9770, 1, 1, 0, 0, AmountFlagEnum.NO.getType()),
    PROFIT_STATUS("text_11", "enum.relativeProduct.attrName.profit_status", "profitStatus", FieldTypeEnum.TEXT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9760, 1, 0, 0, 0, AmountFlagEnum.NO.getType()),
    ;

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 是否是自定义字段
     * 1：是
     * 0：不是
     */
    private Integer isRedundant;
    /**
     * 是否允许编辑
     * 1：是
     * 0：不是
     */
    private Integer editable;
    /**
     * 数字字段为金额类型，1：是；0：不是
     * AmountFlagEnum
     */
    private Integer amountFlag;

    RelativeProductEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                        Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer isRedundant, Integer editable, Integer amountFlag) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.isRedundant = isRedundant;
        this.editable = editable;
        this.amountFlag = amountFlag;
    }

    /**
     * 获取全部枚举
     *
     * @return List<RelativeProductEnum>
     */
    public static List<RelativeProductEnum> getAllEnum() {
        List<RelativeProductEnum> list = new ArrayList<>();
        for (RelativeProductEnum cache : values()) {
            list.add(cache);
        }
        return list;
    }

    /**
     * 根据code获取对应枚举
     *
     * @return RelativeProductEnum
     */
    public static RelativeProductEnum getBySaasAttr(String saasAttr) {
        if (saasAttr == null || "".equals(saasAttr)) {
            return null;
        }
        for (RelativeProductEnum cache : values()) {
            if (Objects.equals(cache.getSaasAttr(), saasAttr)) {
                return cache;
            }
        }
        return null;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

}
